package application;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
import crash.CrashHandler;
import utils.LocationClientUtil;
import utils.SPUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author Andlei
 */
public class App extends MultiDexApplication {
    private static App app;
    private static SharedPreferences preferences;
    public static String storageUrl = Environment.getExternalStorageDirectory().getAbsolutePath();

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;

        CrashHandler crashHandler = CrashHandler.getInstance();
        // 注册crashHandler
        crashHandler.init(getApplicationContext());
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));

        //注册bugly
        Bugly.init(getApplicationContext(), "482f037faa", false);
        //CrashReport.initCrashReport(getApplicationContext(), "482f037faa", false);
        CrashReport.setUserId(SPUtils.getInstance(app).getString("phone"));  //该用户本次启动后的异常日志用户ID都将是9527
//
//        SkinCompatManager.withoutActivity(this)                         // 基础控件换肤初始化
//                .addInflater(new SkinMaterialViewInflater())            // material design 控件换肤初始化[可选]
//                .addInflater(new SkinConstraintViewInflater())          // ConstraintLayout 控件换肤初始化[可选]
//                .addInflater(new SkinCardViewInflater())                // CardView v7 控件换肤初始化[可选]
//                .setSkinStatusBarColorEnable(false)                     // 关闭状态栏换肤，默认打开[可选]
//                .setSkinWindowBackgroundEnable(false)                   // 关闭windowBackground换肤，默认打开[可选]
//                .loadSkin();
//        SkinCompatManager.getInstance().loadSkin("night", SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); // 后缀加载
//        SkinCompatManager.getInstance().loadSkin("night", SkinCompatManager.SKIN_LOADER_STRATEGY_PREFIX_BUILD_IN); // 前缀加载

        //百度地图定位开始
        LocationClientUtil locationClientUtil = new LocationClientUtil();
        locationClientUtil.start();

    }

    public static App getInstance() {
        return app;
    }

    public static SharedPreferences getPreferences() {
        if (preferences == null) {
            preferences = app.getSharedPreferences("SharedPreferences", MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = preferences.edit();


        edit.putString("putString", "string").apply();

        edit.remove("putString").apply();

        edit.clear().apply();

        return preferences;

    }

    /**
     * 获取内部存储路径
     *
     * @return 路径
     */
    public String getDirPath() {
        return getApplicationContext().getFilesDir().getAbsolutePath();
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

}
